package com.example.ffmpegplayer.player;

import com.example.ffmpegplayer.view.MediaGLSurfaceView;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author ly-zhouxyi
 * @date 2024/3/4
 * @time 16:18
 */
public class PlayerImpl implements IPlayer{
    private static final String TAG = PlayerImpl.class.getSimpleName();

    public PlayerImpl() {
        PlayerManager.getInstance().native_init();
    }

    @Override
    public void setDataSource(String path) {
        PlayerManager.getInstance().native_setDataSource(path);
    }

    @Override
    public void setSurfaceView(MediaGLSurfaceView view) {
        PlayerManager.getInstance().setSurfaceView(view);
    }

    @Override
    public void prepareAsync() {
       new Thread(() -> PlayerManager.getInstance().native_prepare()).start();
    }

    @Override
    public void start() {
        PlayerManager.getInstance().native_start();
    }

    @Override
    public void stop() {
        PlayerManager.getInstance().native_stop();
    }

    @Override
    public void pause() {
        PlayerManager.getInstance().native_pause();
    }

    @Override
    public void resume() {
        PlayerManager.getInstance().native_resume();
    }

    @Override
    public void setScreenOnWhilePlaying(boolean screenOn) {

    }

    @Override
    public int getVideoWidth() {
        return 0;
    }

    @Override
    public int getVideoHeight() {
        return 0;
    }

    @Override
    public boolean isPlaying() {
        return false;
    }

    @Override
    public void seekTo(int position) {
        PlayerManager.getInstance().native_seek(position);
    }


    @Override
    public int getDuration() {
        return PlayerManager.getInstance().getDuration();
    }

    @Override
    public void release() {

    }

    @Override
    public void reset() {

    }

    @Override
    public void setChannelMute(int channel) {
        PlayerManager.getInstance().native_setChannelMute(channel);
    }

    @Override
    public void setVolumeMute(boolean mute) {
        PlayerManager.getInstance().native_setVolumeMute(mute);
    }

    @Override
    public void setVolume(int volume) {
        PlayerManager.getInstance().native_setVolume(volume);
    }

    @Override
    public int getCurrentVolume() {


        int volume = PlayerManager.getInstance().native_getVolume();
        BigDecimal n =new BigDecimal(volume);
        BigDecimal m =new BigDecimal(-5000);
        BigDecimal divide = n.divide(m, 2, RoundingMode.HALF_UP);
        float v = (1.0f-divide.floatValue())*100.0f;
        return (int)v;
    }

    @Override
    public void setSpeed(float speed) {
        PlayerManager.getInstance().native_setSpeed(speed);
    }

    @Override
    public void setPitch(float pitch) {
        PlayerManager.getInstance().native_setPitch(pitch);
    }

    @Override
    public void registerIPlayerCallback(IPlayerCallback playerCallback) {
        PlayerManager.getInstance().registerIPlayerCallback(playerCallback);
    }

    @Override
    public void unregisterIPlayerCallback() {
        PlayerManager.getInstance().unregisterIPlayerCallback();
    }
}